package com.develop.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

public class PropertiesUtils {
	private static final Logger log=Logger.getLogger(PropertiesUtils.class);
	
	//配置缓存
	public static final Map<String,HashMap<String,String>> configMap = new HashMap<String,HashMap<String,String>>(); 
	
	/**
	 * 默认文件名称
	 */
	public static final String APP_PROPERTY_FILE_NAME = "application.properties";
	
	
	public static String getPropertiesByKey(String key ,String fileName) {
		
		if(configMap.containsKey(fileName) && configMap.get(fileName).containsKey(key)){
			return configMap.get(fileName).get(key);
		}else{
			InputStream inputStream = PropertiesUtils.class.getClassLoader()
					.getResourceAsStream(fileName);
			Properties p = new Properties();
			try {
				p.load(inputStream);
			} catch (IOException e) {
				log.error(e.getMessage(),e); 
			}
			String value = p.getProperty(key);
			HashMap<String,String> config = new HashMap<String,String>();
			config.put(key, value);
			if(configMap.containsKey(fileName)){
				configMap.get(fileName).put(key, value);
			}else{
				configMap.put(fileName, config);
			}
			return value;
		}
	}
	public static void main(String[] args) {
		String aa=getPropertiesByKey("kafka.consumer.bootstrap.servers", "application.properties");
		System.out.println(aa);
		
	}
	
}
